package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkListV1Service;

@CrossOrigin
@RestController
public class WorklistFindV1Controller extends YxyApiBaseController {

    @Autowired
    private HomeworkListV1Service homeworkListV1Service;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/find.v1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        // 参数处理
        if (reqModel.keyword != null && reqModel.toString().trim().length() > 0) {
        }
        // ##
        // 获取 querytime,isDateDecrement 后第一个有任务的时间
        reqModel.publishDate = homeworkListV1Service.getPublishDate(reqModel);
        // ##
        // 最后任务时间为空，返回 null
        if (reqModel.publishDate == null) { return respModel; }
        // ##
        // 获取某天教师发布任务列表
        List<HomeworkTaskClass> homeworkTaskClasss = homeworkListV1Service.findHomeworkTaskClass(reqModel);
        // ##
        // 对数据进行处理,转化为 RespModel 对象
        respModel = homeworkListV1Service.convertRespModel(homeworkTaskClasss);
        return respModel;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 检索条件
        public String keyword;
        // 时间
        public String querytime;
        // 班级Id
        public String classId;
        // 当前日期是否减一天
        public String isDateDecrement;
        // 学段
        public String stage;
        // 学科
        public String subjectId;
        // 用户Id (不用传参数)
        public String userId;
        // 学校Id (不用传参数)
        public String schoolId;
        // 任务发布时间 (不用传参数)
        public Date publishDate;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        public String year;
        public String month;
        public String day;
        public String week;
        public String dateTime;
        public List<Homework> homeworks;
    }

    public static class Homework {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 作业标题
        public String title;
        // 年级
        public String grade;
        // 班级
        public String className;
        // 总人数
        public int totalPeople;
        // 提交人数
        public int submitPeople;
        // 批阅人数
        public int readoverPeople;
        // 提交人数百分比
        public String submitRatio;
        // 批阅人数百分比
        public String readoverRatio;
        // 正确率
        public String accuracy;
        // 发布时间
        public String publishTime;
        // 截止时间
        public String predictDate;
        // 作业状态 1 已批阅、2 未批阅、 3 批阅中
        public int workStatus;
        // 时间状态
        public String timeStatus;
        // 是否有通知类试题
        public boolean isNotify = false;
        // 通知类作业是否需要学生在线提交true需要,false不需要
        public Boolean ifNeedSumbit = true;
        // 本次作业试题总数
        public int quesTotal = 0;
    }
}
